// ========================================================================
// Copyright (c) 2022 ChangYou, All rights reserved.
// http://www.changyou.com/
// 
// Filename:    GPUMesh.cs
// Time:        2022-08-22 20:51:53
// Author:      xiangjinbao
// Email:       xiangjinbao@cyou-inc.com
// Version:     v2022.0.1
// Description: GPUMesh
// ========================================================================

using System.Collections.Generic;
using UnityEngine;

namespace ProjectTW.Foliage
{
    /// <summary>
    /// 模型对象
    /// </summary>
    public class FoliageGPUMesh
    {
        /// <summary>
        /// 1RD Dimension -> mesh lods index 模型LODS索引
        /// 2RD Dimension -> density lods index 模型所对应密度LODS索引
        /// 3RD Dimension -> Mesh index 模型密度所有下所对应合并模型
        /// </summary>
        public Mesh[,,] MeshesCache;
        
        /// <summary>
        /// Dimension 1 : x chunk
        /// Dimension 2 : z chunk
        /// Dimension 3 : LOD index
        /// </summary>
        public FoliageMeshInstancesGroup MeshInstancesGroup;

        public int MeshLODsCount
        {
            get
            {
                return MeshesCache.GetLength(0);
            }
        }

        public int DensityLODsCount
        {
            get
            {
                return MeshesCache.GetLength(1);
            }
        }

        public int MeshesCount
        {
            get
            {
                return MeshesCache.GetLength(2);
            }
        }

        public FoliageGPUMesh(Mesh[,,] meshesCache, FoliageMeshInstancesGroup meshInstancesGroup, int prototypeIndex, FoliageResolutions resolution)
        {
            this.MeshesCache = meshesCache;
            this.MeshInstancesGroup = meshInstancesGroup;
        }

        public void Destroy()
        {
            for (int i = 0; i < MeshInstancesGroup.Count; i++)
            {
                MeshInstancesGroup.MeshInstanceArray[i].Destroy();
            }

            Mesh mesh;

            for (int a = 0; a < MeshesCache.GetLength(0); a++)
            {
                for (int b = 0; b < MeshesCache.GetLength(1); b++)
                {
                    for (int c = 0; c < MeshesCache.GetLength(2); c++)
                    {
                        mesh = MeshesCache[a, b, c];

                        if (mesh == null)
                        {
                            continue;
                        }

                        Object.DestroyImmediate(mesh);
                    }
                }
            }

            MeshesCache = null;
            MeshInstancesGroup = null;
        }

        public int GetMesh(int density, FoliagePrototype prototype)
        {
            if (density == 0)
            {
                return -1;
            }

            List<byte> lodLevels = prototype.DensitiesLods;

            for (int i = lodLevels.Count - 1; i >= 0; i--)
            {
                if (density <= lodLevels[i])
                {
                    return i;
                }
            }

            return 0;
        }
    }
}
